package com.yanqu.road.server.logic.activity.condition.normal;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.eActivityChargeType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;

import java.math.BigInteger;
import java.util.List;

/**
 * 累充活动
 */
public class RechargeActivityCondition extends BaseActivityCondition {

	public RechargeActivityCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
		super(activityInfo, activityConditionInfo, baseActivityData);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.Recharge.getValue(), handle);
		if(activityInfo.getType() == eActivityType.RechargeActivity.getValue()){
			if(activityInfo.getChildType() == 1){//累计充值
				player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.BackendTotalRecharge.getValue(), handle);
			}else if(activityInfo.getChildType() == 2){//每日充值
				player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.BackendDayRecharge.getValue(), handle);
			}else if(activityInfo.getChildType() == 9){//每日充值插入
				player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.BackendDayRecharge.getValue(), handle);
			}else if(activityInfo.getChildType() == 12){//每日充值替换
				player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.BackendDayRecharge.getValue(), handle);
			}
		}
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.Recharge.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
	}

	@Override
	protected void player_BoardLevelHandle(Object object) {
		if (activityInfo.getChildType() == eActivityChargeType.EveryDayCharge.getChildType()) {
			//判断是否有每次充值活动C
			List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.RechargeActivity.getValue());
			if (activityInfoList != null) {
				for (ActivityInfo info : activityInfoList) {
					if (info.getChildType() == eActivityChargeType.EveryDayChargeReplace.getChildType() && ActivityMgr.activityInTime(info)) {
						return;
					}
				}
			}
		}
		long args = (long)object;
		BigInteger realValue = BigInteger.valueOf(specialActivityGetRealValue(args));
		if (getValue().add(realValue).compareTo(BigInteger.ONE) < 0) {
			setValue(BigInteger.ZERO);
		}else{
			setValue(getValue().add(realValue));
		}
	}

	@Override
	public void initRechargeConditionData() {
		player.getModule(EarnSpeedModule.class).resetDay();
		if (getValue().compareTo(BigInteger.ZERO) == 0) {
			long money = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.DayTotalRecharge);
			if (money > 0) {
				changeValue(money);
			}
		}
	}


}
